using System;
using System.Collections.Generic;
using System.Text;

using System.Management.Automation;

namespace Microsoft.PowerShell.CmdletManagement
{
    [Cmdlet(VerbsCommon.Set, "PsSpecNoun", SupportsShouldProcess = true)]
    public class SetPsSpecNounCommand : PsSpecCommandBase
    {
        [Parameter(Mandatory = true, Position = 0, ValueFromPipelineByPropertyName = true)]
        public int Id
        {
            get
            {
                return _id;
            }
            set
            {
                _id = value;
            }
        }
        private int _id = -1;

        [Parameter(Mandatory = false, Position = 1, ValueFromPipelineByPropertyName = true)]
        [ValidateLength(1, 50)]
        public String Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }
        private string _name = null;

        [Parameter(Mandatory = false, Position = 2, ValueFromPipelineByPropertyName = true)]
        [ValidateLength(1, 200)]
        public String Definition
        {
            get
            {
                return _definition;
            }
            set
            {
                _definition = value;
            }
        }
        private string _definition = null;

        protected override void ProcessRecord()
        {
            // Check that they've specified a valid name
            if ((! String.IsNullOrEmpty(Name)) && (! PsSpecCmdletUtils.IsValidNounName(Name)))
            {
                WriteError(new ErrorRecord(
                    new Exception(resources.InvalidNounName),
                    "INVALID_NOUN_NAME",
                    ErrorCategory.InvalidArgument, Name));
                return;
            }

            if (ShouldProcess(
                String.Format("{0}", ServiceUri),
                String.Format(resources.UpdateNounShouldProcess, Id, Name, Definition)))
            {
                // Update the noun
                try
                {
                    Service.UpdateNoun(Id, Name, Definition);
                }
                catch (System.Web.Services.Protocols.SoapException e)
                {
                    WriteError(new ErrorRecord(e, "PROJECT_SERVICE_ERROR", ErrorCategory.InvalidOperation, Name));
                }
            }
        }
    }
}
